﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ContractManagermentSystem.Common;
using ContractManagermentSystem.Entity;
using System.Diagnostics;

namespace ContractManagermentSystem.DataAccess
{
    public class ContractorDao
    {
        private readonly ConnectionHelper _connectionHelper;

        public ContractorDao()
        {
            _connectionHelper = new ConnectionHelper();
        }

        public DataTable LoadAllContractor()
        {
            return _connectionHelper.ExecuteDataTable("sp_ViewContractor", null);
        }

        public int CheckUserName(string userName)
        {
            var parameter = new SqlParameter("@Username", SqlDbType.NVarChar, 30) { Value = userName };

            return _connectionHelper.ExecuteScalar("sp_CheckUserNameExisted", parameter);
        }

        public DataTable GetContractorByContractorId(Guid contractorId)
        {
            var parameter = new SqlParameter("@ContractorId", SqlDbType.UniqueIdentifier) { Value = contractorId };

            return _connectionHelper.ExecuteDataTable("sp_GetContractorByContractorId", parameter);
        }

        public void CreateContractor(Contractor contractor)
        {
            var parameters = new SqlParameter[9];

            parameters[0] = new SqlParameter("@Username", SqlDbType.VarChar, 50) { Value = contractor.Username };
            parameters[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50) { Value = contractor.Password };
            parameters[2] = new SqlParameter("@FullName", SqlDbType.VarChar, 50) { Value = contractor.FullName };
            parameters[3] = new SqlParameter("@PhoneNumber1", SqlDbType.VarChar, 20) { Value = contractor.PhoneNumber1 };
            parameters[4] = new SqlParameter("@PhoneNumber2", SqlDbType.VarChar, 20) { Value = contractor.PhoneNumber2 };
            parameters[5] = new SqlParameter("@Gender", SqlDbType.VarChar, 20) { Value = contractor.Gender };
            parameters[6] = new SqlParameter("@IsActive", SqlDbType.Bit) { Value = contractor.IsActive };
            parameters[7] = new SqlParameter("@Branch", SqlDbType.Char, 10) { Value = contractor.Branch };
            parameters[8] = new SqlParameter("@SortCode", SqlDbType.Char, 10) { Value = contractor.SortCode };
            _connectionHelper.ExecuteNonQuery("sp_CreateContractor", parameters);
        }

        public void UpdateContractor(Contractor contractor)
        {
            var parameters = new SqlParameter[6];
            parameters[0] = new SqlParameter("@ContractorId", SqlDbType.UniqueIdentifier) { Value = contractor.ContractorId };
            parameters[1] = new SqlParameter("@PhoneNumber1", SqlDbType.VarChar, 20) { Value = contractor.PhoneNumber1 };
            parameters[2] = new SqlParameter("@PhoneNumber2", SqlDbType.VarChar, 20) { Value = contractor.PhoneNumber2 };
            parameters[3] = new SqlParameter("@Gender", SqlDbType.VarChar, 20) { Value = contractor.Gender };
            parameters[4] = new SqlParameter("@Branch", SqlDbType.Char, 10) { Value = contractor.Branch };
            parameters[5] = new SqlParameter("@SortCode", SqlDbType.Char, 10) { Value = contractor.SortCode };
            _connectionHelper.ExecuteNonQuery("sp_UpdateContractor", parameters);
        }
    }
}
